import request from '../../utils/request';
import config from '../../config';
Page({
    data: {
        baseUrl: config.BASE_URL,
        config: wx.getStorageSync('config'),
        balance: 84.88, // 当前余额
        applyAmount: '', // 用户输入的申请金额
        inputPlaceholder: '输入金额', // 输入框的placeholder
    },
    onLoad: function (options) {
        this.setData({
            balance: wx.getStorageSync('userInfo').freezeDeposits
        })
    },
    onShow: function () { },
    // 输入金额处理
    handleAmountInput: function (e) {
        let value = e.detail.value;
        // 简单校验，只允许数字和小数点，且小数点后最多两位
        value = value.replace(/[^\d.]/g, ''); // 清除非数字和.
        if (value.indexOf('.') > -1) {
            value = value.substring(0, value.indexOf('.') + 3); // 保留两位小数
        }
        this.setData({
            applyAmount: value
        });
        // 返回处理后的值，确保输入框同步更新
        return value;
    },
    // 点击"全部"按钮
    handleApplyAll: function () {
        this.setData({
            applyAmount: this.data.balance.toString() // 将全部余额填入输入框
        });
    },
    // 点击"确认申请"按钮
    handleConfirmApply: function () {
        const amount = parseFloat(this.data.applyAmount);
        if (isNaN(amount) || amount <= 0) {
            wx.showToast({
                title: '请输入有效的提现金额',
                icon: 'none'
            });
            return;
        }
        if (amount > this.data.balance) {
            wx.showToast({
                title: '提现金额不能超过当前余额',
                icon: 'none'
            });
            return;
        }

        console.log('用户申请提现金额：', amount);
        wx.showLoading({
            title: '正在提交...',
        });
        request({
            url: 'api/myUser/withdrawal',
            method: 'post',
            data: {
                amount: amount
            }
        }).then(res => {
            wx.hideLoading();
            wx.showToast({
                title: '申请已提交',
                icon: 'success'
            });
            // 可以在成功后清空输入框或跳转页面
            this.setData({
                applyAmount: '',
                balance: (Number(this.data.balance) - Number(amount)).toFixed(2)
            });

        });
    }
}); 